<?php

namespace plugin\property\app\model;

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use plugin\enterprise\app\Enterprise;
use plugin\owladmin\app\model\AdminRole;
use plugin\owladmin\app\model\AdminUser;

class PropertyUser extends \plugin\owladmin\app\model\AdminUser
{
    protected $table = 'property_admin_users';


    public function __construct(array $attributes = [])
    {
        $this->setConnection(Enterprise::config('property.database.connection'));

        parent::__construct($attributes);
    }

    public function roles(): BelongsToMany
    {
        return $this->belongsToMany(PropertyRole::class, 'property_admin_role_users', 'user_id', 'role_id')->withTimestamps();
    }

    protected static function boot(): void
    {
        parent::boot();
        static::deleting(function (PropertyUser $model) {
            $model->roles()->detach();
        });
    }
}